package cn.gemiman.daoyi.module.iot.controller.admin.userequipmentplace;

import cn.gemiman.daoyi.framework.apilog.core.annotation.ApiAccessLog;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import cn.gemiman.daoyi.framework.excel.core.util.ExcelUtils;
import cn.gemiman.daoyi.module.iot.controller.admin.userequipmentplace.vo.UserEquipmentPlacePageReqVO;
import cn.gemiman.daoyi.module.iot.controller.admin.userequipmentplace.vo.UserEquipmentPlaceRespVO;
import cn.gemiman.daoyi.module.iot.controller.admin.userequipmentplace.vo.UserEquipmentPlaceSaveReqVO;
import cn.gemiman.daoyi.module.iot.dal.dataobject.userequipmentplace.UserEquipmentPlaceDO;
import cn.gemiman.daoyi.module.iot.service.userequipmentplace.UserEquipmentPlaceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.gemiman.daoyi.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 会员设备场所")
@RestController
@RequestMapping("/iot/user-equipment-place")
@Validated
public class UserEquipmentPlaceController {

    @Resource
    private UserEquipmentPlaceService userEquipmentPlaceService;

    @PostMapping("/create")
    @Operation(summary = "创建会员设备场所")
    @PreAuthorize("@ss.hasPermission('iot:user-equipment-place:create')")
    public CommonResult<Long> createUserEquipmentPlace(@Valid @RequestBody UserEquipmentPlaceSaveReqVO createReqVO) {
        return success(userEquipmentPlaceService.createUserEquipmentPlace(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员设备场所")
    @PreAuthorize("@ss.hasPermission('iot:user-equipment-place:update')")
    public CommonResult<Boolean> updateUserEquipmentPlace(@Valid @RequestBody UserEquipmentPlaceSaveReqVO updateReqVO) {
        userEquipmentPlaceService.updateUserEquipmentPlace(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员设备场所")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:user-equipment-place:delete')")
    public CommonResult<Boolean> deleteUserEquipmentPlace(@RequestParam("id") Long id) {
        userEquipmentPlaceService.deleteUserEquipmentPlace(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员设备场所")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:user-equipment-place:query')")
    public CommonResult<UserEquipmentPlaceRespVO> getUserEquipmentPlace(@RequestParam("id") Long id) {
        UserEquipmentPlaceDO userEquipmentPlace = userEquipmentPlaceService.getUserEquipmentPlace(id);
        return success(BeanUtils.toBean(userEquipmentPlace, UserEquipmentPlaceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员设备场所分页")
    @PreAuthorize("@ss.hasPermission('iot:user-equipment-place:query')")
    public CommonResult<PageResult<UserEquipmentPlaceRespVO>> getUserEquipmentPlacePage(@Valid UserEquipmentPlacePageReqVO pageReqVO) {
        PageResult<UserEquipmentPlaceDO> pageResult = userEquipmentPlaceService.getUserEquipmentPlacePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserEquipmentPlaceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员设备场所 Excel")
    @PreAuthorize("@ss.hasPermission('iot:user-equipment-place:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserEquipmentPlaceExcel(@Valid UserEquipmentPlacePageReqVO pageReqVO,
                                              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserEquipmentPlaceDO> list = userEquipmentPlaceService.getUserEquipmentPlacePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会员设备场所.xls", "数据", UserEquipmentPlaceRespVO.class,
                BeanUtils.toBean(list, UserEquipmentPlaceRespVO.class));
    }

}
